import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.openxml4j.exceptions.*;
import java.applet.*;
import java.net.*;

//uses poi-3.7:
//export CLASSPATH=.:./poi-3.7/poi-3.7-20101029.jar:./poi-3.7/poi-ooxml-3.7-20101029.jar 
class Util {
   public static final Random random=new Random();

   public static List<Card> readExcel(String fileName) {
      Workbook wb=null; 
      try {
         InputStream inp = new FileInputStream("red_bible.xls");
         wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(inp);
      } catch(IOException e) { 
         e.printStackTrace(); 
      } catch(InvalidFormatException e) {
         e.printStackTrace();
      }

      Sheet sheet = wb.getSheetAt(0);
      final int last=sheet.getLastRowNum();

      List<Card> entries=new ArrayList<Card>();
      for(int index=0; index<last; index++) {
         final Row row=sheet.getRow(index);
         final Cell wordCell=row.getCell(0);
         if(wordCell==null) continue;
         //System.out.print(wordCell.getStringCellValue());
         //System.out.print(" ");
         final Cell meaningCell=row.getCell(2);
         if(meaningCell==null) continue;
         //System.out.println(meaningCell.getStringCellValue());
         entries.add(new Card(wordCell.getStringCellValue(), 
            meaningCell.getStringCellValue()));
      }
      /*
      for(String word: entries.keySet()) {
         System.out.print(word);
         System.out.print(" ");
         System.out.println(entries.get(word));
      }
      */
      return entries;
   }

   public static void main(String[] args) throws Exception {
      //readExcel("red_bible.xls");
      //play("/Users/ken/code/gre-memo/test.wav");
      play("sounds/Temple.wav");
   }

   private static final Applet applet=new Applet();

   public static AudioClip getAudioClip(String fileName) {
      try {
         File file=new File(fileName);
         AudioClip clip=applet.newAudioClip(file.toURI().toURL());
         return clip;
      } catch(Exception e) { e.printStackTrace(); }
      throw new RuntimeException();
   }
   public static void play(String fileName) throws Exception {
      //final URL soundToPlay = fileName.getClass().getResource(fileName);
      final File file=new File(fileName);
      //System.out.println(file.exists());
      final AudioClip clip=applet.newAudioClip(file.toURI().toURL());
      clip.play();
   }
}
